package com.bootdo.clouddo.service.book.impl;

import com.bootdo.clouddo.dao.book.BookBookshelfMapper;
import com.bootdo.clouddo.domain.book.BookBookshelf;
import com.bootdo.clouddo.service.book.IBookBookshelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BookBookshelfServiceImpl implements IBookBookshelfService {

    @Autowired
    private BookBookshelfMapper dao;

    @Override
    public int star(Long uid, Long bookId, Map<String, String> data) {
        if(dao.existsByUid(uid, bookId)) {
            dao.deleteByUid(uid, bookId);
            return -1;
        }
        BookBookshelf record = new BookBookshelf();
        record.setBookId(bookId);
        record.setUid(uid);
        record.setAddTime(new Date());
        record.setBookName(data.get("name"));
        record.setCover(data.get("cover"));
        return dao.insertSelective(record);
    }

    @Override
    public List<BookBookshelf> findByUid(Long uid) {
        return dao.findByUid(uid);
    }

    @Override
    public boolean existsByUid(Long uid, Long bookId) {
        return dao.existsByUid(uid, bookId);
    }
}
